package com.kangye.enterprise.controller;

import com.kangye.enterprise.entity.Attention;
import com.kangye.enterprise.entity.Items;
import com.kangye.enterprise.entity.User;
import com.kangye.enterprise.entity.Users;
import com.kangye.enterprise.service.UserService;
import com.kangye.enterprise.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: enterprise
 * @description: 用户Controller
 * @author: 索腾龙
 * @create: 2021-05-26 10:14
 **/
@Controller
public class UserController {
    //注入UserService逻辑接口
    @Autowired
    private UserService userService;
    //跳转登陆页面
    @RequestMapping("/Login")
    public String Login(){
        return "login";
    }
    //登陆
    @RequestMapping("/login")
    public String login(HttpSession httpSession, String userUsername, String userPassword){
        //用实体类User去接收sqlsession里的数据库操作方法
        User user = userService.Login(userUsername);
        if (user!=null){
            //判断输入的密码是否等于数据库的密码  如果相等
            if (user.getUserPassword().equals(userPassword)){
                //将获取的信息存入session里
                httpSession.setAttribute("user",user);
                //跳首页
                return "homepage";
            }else {
                //失败
                return "index1";
            }
        }else {
            return "index2";
        }
    }
    //用户管理模块跳转页面
    @RequestMapping("/selUser")
    public String seluser(){
        return "seluser";
    }
    //用户管理分页查找
    @ResponseBody
    @RequestMapping("/selUserList")
    public ViewJsonObject<User> selUserList(Integer page, Integer limit, User user, String demoReload){
        if (demoReload!=null){
            String [] str = demoReload.split(",");
            for (int i = 0;i<str.length;i++){
                //如果数组长度等于3的时候,3个参数
                if (str.length==3){
                    user.setUserUsername(str[0]);
                    user.setUserRolename(str[1]);
                    user.setUserPhone(str[2]);
                    //长度等于2的时候,2个参数
                }else if (str.length==2){
                    user.setUserUsername(str[0]);
                    user.setUserRolename(str[1]);
                    //长度等于1,1个参数
                }else if (str.length==1){
                    user.setUserUsername(str[0]);
                }

            }
            return userService.selUser(page,limit,user);
        }else {
            return userService.selUser(page,limit,user);
        }
    }
    //添加信息
    @ResponseBody
    @RequestMapping("/addUser")
    public String addUser(User user){
        Integer code = userService.addUser(user);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
    //删除信息
    @ResponseBody
    @RequestMapping("/deleteUserById")
    public ViewJsonObject<User> deleteUserById(Integer id){
        //返回Service方法
       return userService.deleteUserById(id);
    }
    //修改用户信息
    @ResponseBody
    @RequestMapping("/updUser")
    public String updUser(User user){
        Integer code = userService.updUser(user);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
    //重置密码
    @ResponseBody
    @RequestMapping("/updUserById")
    public ViewJsonObject<User> updUserById(Integer id){
        //返回Service方法
        return userService.updUserById(id);
    }
    //分配角色
    @ResponseBody
    @RequestMapping("/updUserByRoleId")
    public String updUserByRoleId(Integer userRoleid, Integer id){
        //返回Service方法
        Integer code = userService.updUserByRoleId(userRoleid,id);
        if (code>0){
            if (userRoleid == 2){
                return "200";
            }else if (userRoleid==3){
                return "201";
            }else {
                return "202";
            }
        }else {
            return "203";
        }
    }
    //批量删除
    @ResponseBody
    @RequestMapping("/deleteUserByIds")
    public ViewJsonObject<User> deleteUserByIds(String ids){
        List<String> list = new ArrayList<>();
        String[] str = ids.split(",");
        for (String st:str){
            list.add(st);
        }
        return userService.deleteUserByIds(list);
    }
    //跳转角色用户页面
    @RequestMapping("/selRole")
    public String selRole(){
        return "selRole";
    }
    //跳转账号管理页面
    @RequestMapping("/accountManage")
    public String accountManage(){
        return "accountManage";
    }
    //修改密码接口
    @ResponseBody
    @RequestMapping("/updPassword")
    public String updPassword(User user){
        Integer code = userService.updPassword(user);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
    //跳转用户信息页面
    @RequestMapping("/selUsers")
    public String selUsers(){
        return "selUsers";
    }
    //查看app用户信息
    @ResponseBody
    @RequestMapping("/selUsersList")
    public ViewJsonObject<Users> selUsers(Integer page, Integer limit,String usersRolename){
        return userService.selUsers(page, limit,usersRolename);
    }
    //查询用户申报多少条
    @ResponseBody
    @RequestMapping("/selItemsCount")
    public String selItemsCount(Integer id) {
        return "查看"+ " "+"("+userService.selItemsCount(id)+")";
    }
    //查询用户关注多少条
    @ResponseBody
    @RequestMapping("/selAttentionCount")
    public String selAttentionCount(Integer id) {
        return "查看"+ " "+"("+userService.selAttentionCount(id)+")";
    }
    //查看用户申报跳转页面
    @RequestMapping("/selItemsByIdes")
    public String selItemsByIds(){
        return "shenbao";
    }
    //查看用户申报的项目
    @ResponseBody
    @RequestMapping("/selItemsByIds")
    ViewJsonObject<Items> selItemsByIds(Integer page, Integer limit, Integer id){
        return userService.selItemsByIds(page, limit, id);
    }
    //查看用户的关注信息跳转页面
    @RequestMapping("/selAttentionByIdss")
    public String selAttentionByIds(){
        return "guanzhu";
    }
    //查看用户的关注信息
    @ResponseBody
    @RequestMapping("/selAttentionByIds")
    ViewJsonObject<Attention> selAttentionByIds(Integer page, Integer limit, Integer id){
        return userService.selAttentionByIds(page, limit, id);
    }

    //跳转首页显示欢迎页面
    @RequestMapping("/welCome")
    public String welCome(){
        return "welcome";
    }
    //退出系统
    @RequestMapping("/goOut")
    public String goOut(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return "forward:Login";
    }
}